# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=wluma
pkgver=4.1.2
pkgrel=2
pkgdesc="Automatic brightness adjustment based on screen contents and ALS"
url="https://github.com/maximbaz/wluma"
# riscv64, s390x: blocked by rust/cargo
# arm*, x86: fails to build due to crappy v4l crate
arch="aarch64 ppc64le x86_64"
license="ISC"
makedepends="
	cargo
	clang
	eudev-dev
	v4l-utils-dev
	wayland-dev
	vulkan-loader-dev
	"
install="$pkgname.post-install"
subpackages="$pkgname-wlroots"
_libv4l_gitrev=d7ac716f6065a0e1aa47ef673387603d6eacc4f0
source="https://github.com/maximbaz/wluma/archive/$pkgver/wluma-$pkgver.tar.gz
	cargo.patch
	link-vulkan-at-runtime.patch
	sys-level-config.patch
	default-config.patch

	https://github.com/raymanfx/libv4l-rs/archive/$_libv4l_gitrev/libv4l-rs-$_libv4l_gitrev.tar.gz
	libv4l-rs-01-fix-string-pointer-cast.patch
	libv4l-rs-02-fix-wrong-_IOC_TYPE-on-musl.patch
	"

prepare() {
	mv ../libv4l-rs-$_libv4l_gitrev "$builddir"/libv4l-rs

	default_prepare

	# Optimize binary for size.
	cat >> Cargo.toml <<-EOF

		[profile.release]
		codegen-units = 1
		lto = true
		opt-level = "s"
		panic = "abort"
	EOF

	mv config.toml config.toml.example

	# Create a default empty config that will be embedded in the executable.
	echo '[als.none]' > config.toml

	cargo fetch --locked
}

build() {
	cargo build --frozen --release
}

check() {
	cargo test --frozen
}

package() {
	install -D -m755 target/release/wluma -t "$pkgdir"/usr/bin/
	install -D -m644 90-wluma-backlight.rules -t "$pkgdir"/lib/udev/rules.d/
	install -D -m644 config.toml.example "$pkgdir"/etc/xdg/wluma/config.toml
}

wlroots() {
	pkgdesc="$pkgdesc (wlroots capturer)"
	depends="$pkgname=$pkgver-r$pkgrel vulkan-loader wlroots"
	install_if="$pkgname=$pkgver-r$pkgrel wlroots"

	mkdir -p "$subpkgdir"
}

sha512sums="
d9e16f0af0961678c9f59fd692f50e517cf0f5791d16c50314ee909cab81b468c921850d50f264199cf4b52388d3e30797d7cc82ab22d71f26ee3e5705157992  wluma-4.1.2.tar.gz
56fef5b5cdc4d1e7fbf4c592ee7235cab9c4512a8f87d4c83c8d1b2309d6aaf4dd758516b4f32b35823ddfe162787740881202eb2f2b072df6b479dfe3e7295f  cargo.patch
9e6d773e41fa40b9f565d98929a7c561e37e641fcb1ffdfd4295cdf94e324be467e82016d002b86132b0c4293e265703ee0f05d1f061929c17bf08e34f4bedfc  link-vulkan-at-runtime.patch
cecbb642c881c17edc2dd8475905db186a55f1026a16fb8049b5597b69f74b8db3d9c705f5e0ac75351955347f534d545e8069dee10ce5b378aa60c9b9be45aa  sys-level-config.patch
d6d864b048b711699d612aa3d658ada8c1e7094271885dccddec0064bebe4380bb014a74ef0cd3f9700d6964b20b268b2c6966000a6cb98c63110729a133f041  default-config.patch
7b60a001e695b7b7281807c2d96ad640d1072093a97279299f3051bfae6eed32e23ddc72dbcd0a06416cd5e8497012b13a0c3dac43ae7ec1b79cc77c3f1e2599  libv4l-rs-d7ac716f6065a0e1aa47ef673387603d6eacc4f0.tar.gz
828075e7bec0317c137ce0d68424d209cdf62ff24c55e813f4449401de884643bd9e6114acbf92f002459f4a95d4d672bab4eba03216351319b7f0366b3006f3  libv4l-rs-01-fix-string-pointer-cast.patch
62d81304209f072cb63073b306e6b61fbd0e7cd0c1cde75f1c24e792385c437e1c6737ec3713b9f71bae0c9b37ad47b08605b5c4af6d6ebf78d5fc9a9628fea5  libv4l-rs-02-fix-wrong-_IOC_TYPE-on-musl.patch
"
